﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using ElectronicJournal.DB;
using ElectronicJournal.DB.Item;

namespace ElectronicJournal.Admin
{
    public partial class Theme : System.Web.UI.Page
    {
        private ItemTheme oldtheme;
        protected void Page_Load(object sender, EventArgs e)
        {
            DBTheme dbTheme = new DBTheme();
            oldtheme = dbTheme.GetTheme();
            if (!IsPostBack)
            {
                idcolor.Value = oldtheme.color;
                idbgcolor.Value = oldtheme.backgroundcolor;
                idbgcolormenu.Value = oldtheme.bgcolormenu;
                idbgcolormenuitem.Value = oldtheme.bgcolormenuitem;
                idbordercolormenuitem.Value = oldtheme.bordercolormenuitem;
                idcolormenuitem.Value = oldtheme.colormenuitem;
                idbgcolorfooter.Value = oldtheme.bgcolorfooter;
            }
        }


        protected void btnReset_Click(object sender, EventArgs e)
        {
            DBTheme dbTheme = new DBTheme();
            ItemTheme theme = new ItemTheme();
            theme.color = idcolor.Value = "151B39";
            theme.backgroundcolor = idbgcolor.Value = "D8E3FA";
            theme.bgcolormenu = idbgcolormenu.Value = "352A70";
            theme.bgcolormenuitem = idbgcolormenuitem.Value = "564572";
            theme.bordercolormenuitem = idbordercolormenuitem.Value = "5F4C7E";
            theme.colormenuitem = idcolormenuitem.Value = "dde4ec";//"D8E3FA";
            theme.bgcolorfooter = idbgcolorfooter.Value = "45296F";

            dbTheme.Update(theme);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DBTheme dbTheme = new DBTheme();
            ItemTheme theme = new ItemTheme();

            theme.color = idcolor.Value;
            theme.backgroundcolor = idbgcolor.Value;
            theme.bgcolormenu = idbgcolormenu.Value;
            theme.bgcolormenuitem = idbgcolormenuitem.Value;
            theme.bordercolormenuitem = idbordercolormenuitem.Value;
            theme.colormenuitem = idcolormenuitem.Value;
            theme.bgcolorfooter = idbgcolorfooter.Value;
            theme.backgroundimage = SaveFileUpload(fupbg);
            theme.logoheader = SaveFileUpload(fuptop);
            theme.bgimgtopleft = SaveFileUpload(fuptopleft);
            theme.bgimgtopright = SaveFileUpload(fuptopright);
            theme.bgimgright = SaveFileUpload(fupright);
            theme.bgimgleft = SaveFileUpload(fupleft);
            theme.bgimgbottomright = SaveFileUpload(fupbottomright);
            theme.bgimgbottomleft = SaveFileUpload(fupbottomleft);
            theme.bgimgbottom = SaveFileUpload(fupbottom);
            if (theme.backgroundimage == "") theme.backgroundimage = oldtheme.backgroundimage;
            if (theme.logoheader == "") theme.logoheader = oldtheme.logoheader;
            if (theme.bgimgtopleft == "") theme.bgimgtopleft = oldtheme.bgimgtopleft;
            if (theme.bgimgtopright == "") theme.bgimgtopright = oldtheme.bgimgtopright;
            if (theme.bgimgright == "") theme.bgimgright = oldtheme.bgimgright;
            if (theme.bgimgleft == "") theme.bgimgleft = oldtheme.bgimgleft;
            if (theme.bgimgbottomright == "") theme.bgimgbottomright = oldtheme.bgimgbottomright;
            if (theme.bgimgbottomleft == "") theme.bgimgbottomleft = oldtheme.bgimgbottomleft;
            if (theme.bgimgbottom == "") theme.bgimgbottom = oldtheme.bgimgbottom;
            dbTheme.Update(theme);

        }

        private string SaveFileUpload(FileUpload fileupload)
        {
            string filename = "";
            string location;

            if (fileupload.PostedFile != null && fileupload.PostedFile.ContentLength > 0)
            {
                filename = Path.GetFileName(fileupload.PostedFile.FileName);
                location = Server.MapPath("/Styles") + "\\" + filename;

                try
                {
                    fileupload.PostedFile.SaveAs(location);
                }
                catch (Exception ex)
                {
                }

            }

            return filename;
        }

    }
}